package cn.lsq.org.controller;

import cn.lsq.basic.utils.AjaxResult;
import cn.lsq.basic.utils.PageList;
import cn.lsq.org.domain.Shop;
import cn.lsq.org.query.ShopQuery;
import cn.lsq.org.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private ShopService shopService;
    @GetMapping("/{id}")
    public Shop getOne(@PathVariable("id")Long id){
        return shopService.loadById(id);
    }

    @GetMapping
    public List<Shop> getAll(){
        return shopService.loadAll();
    }

    @PostMapping
    public PageList<Shop> getPage(@RequestBody ShopQuery shopQuery){
        return shopService.queryPage(shopQuery);
    }

    @PutMapping
    public AjaxResult saveAndUpdate(@RequestBody Shop shop){
        try {
            if (shop.getId()==null){
                shopService.save(shop);
            }else {
                shopService.update(shop);
            }
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"fuck");
        }


    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            shopService.remove(id);
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"fuck");
        }
    }
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
            shopService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"fuck");
        }
    }
}
